#
# SetLabelDialogs.py
#
# $Id: SetLabelDialogs.py,v 1.1 1999/07/12 08:39:41 toni Exp $
#
# this class implements the dialogs for setting a label on a version
#
# Author: Uwe Stieber
# Copyright (c) 1998, 1999 TakeFive Software, Salzburg
#

# some system modules
import sys
import os
import string

# debug variable
from vcs import TEST

# some SNiFF+ modules
from etgui import *
from PageLayout import *

# some global variables

# builds the set label dialog
def ShowLabelDialog(title, defaultLabel, defaultVersion, tool):
	model = PotGuiModel()
	model.SetValue("Label", defaultLabel)
	model.SetValue("Version", defaultVersion)
	model.SetValue("floating", 0)
	model.SetValue("sniff_cs", 0)
	# version textfield
	if defaultVersion == "TIP" or defaultVersion == "HEAD":
		versionField = TextItem(model.GetValue("Version"))
	else:
		versionField = PotTextField("Version")
	if string.upper(tool) == "PVCS":
		optionBox = VBox(
						items=[
							PotGuiToggleButton("floating", "Set as floating label"),
							PotGuiToggleButton("sniff_cs", "Set as SNiFF+ changeset")
						] # items
					) # VBox
	else:
		optionBox = VBox(
						items=[
							PotGuiToggleButton("sniff_cs", "Set as SNiFF+ changeset")
						] # items
					) # VBox
	retval=ShowSubDialog("%s" % title,
		model,
		MakePageLayout(eVObjHExpand | eVObjVExpand,
			MakeSectionLayout(
				Form(
					items=[
						"Label:", PotTextField("Label"),
						"Revision:", versionField
					]	# items
				)	# Form
			),	# MakeSectionLayout
			MakeBorderedSectionLayout("Options",
				Form(
					items=[
						optionBox
					]	# items
				)	# Form
			)	# MakeBorderedSectionLayout
		) # MakePageLayout
	)	# ShowSubDialog
	if TEST: print "(@B@I%s@I@B)\tShowLabelDialog @BretValue: @b%d@x@B" % (__name__, retval)
	if retval:
		newLabel = model.GetValue("Label")
		newVersion = model.GetValue("Version")
		floatingFlag = model.GetValue("floating")
		changesetFlag = model.GetValue("sniff_cs")
		return (newLabel, newVersion, floatingFlag, changesetFlag)
	else:
		return (None, None, 0, 0)
	
def Show(revision, tool):
	if TEST: print "(@B@I%s@I@B)@B@g>>>@x Show@B" % __name__
	title = "Set Label on Revision"
	defaultLabel = ""
	defaultVersion = string.upper(revision)
	label, version, floatingFlag, changesetFlag = ShowLabelDialog(title, defaultLabel, defaultVersion, tool)
	if label != None and version != None:
		if TEST:
			print "(@B@I%s@I@B)\t@BDialog result: label = '@b%s@x'; version = '@b%s@x'@B; Floating Label = @B@b%d@x@B; Make Changeset = @B@b%d@x@B" % (__name__, label, version, floatingFlag, changesetFlag)
	if TEST: print "(@B@I%s@I@B)@B@r<<<@x Show@B" % __name__
	return (label, version, floatingFlag, changesetFlag)

# builds the move label dialog
def ShowMoveLabelDialog(aFileName, aLabelList, revisionList):
	model = PotGuiModel()
	model.SetValue("SelectedRevision", "")
	if len(aLabelList) > 1:
		model.SetValue("SelectedLabel", "")
		labelField = PotGuiPopupButton("SelectedLabel")
		firstSingleLabel = ""
		for aSingleLabel in aLabelList:
			if firstSingleLabel == "":
				firstSingleLabel = aSingleLabel
			labelField.AddItem(aSingleLabel, aSingleLabel)
		model.SetValue("SelectedLabel", firstSingleLabel)
	else:
		model.SetValue("Label", aLabelList[0])
		labelField = TextItem(model.GetValue("Label"))
	revisionField = PotGuiPopupButton("SelectedRevision")
	firstSingleRevision = ""
	for aItem in revisionList:
		if firstSingleRevision == "":
			firstSingleRevision = aItem
		revisionField.AddItem(aItem, aItem)
	model.SetValue("SelectedRevision", firstSingleRevision)
	retval=ShowSubDialog("Move Label of File %s" % aFileName,
		model,
		MakePageLayout(eVObjHExpand | eVObjVExpand,
			MakeSectionLayout(
				Form(
					items=[
						HBox(
							items=[
								"Move label", labelField,
								"to Revision", revisionField
							] # items
						) # HBox
					]	# items
				)	# Form
			)	# MakeSectionLayout
		) # MakePageLayout
	)	# ShowSubDialog
	if TEST: print "(@B@I%s@I@B)\tShowMoveDialog @BretValue: @b%d@x@B" % (__name__, retval)
	if retval:
		selectedRevision = model.GetValue("SelectedRevision")
		if len(aLabelList) > 1:
			selectedLabel = model.GetValue("SelectedLabel")
		else:
			selectedLabel = model.GetValue("Label")
		if TEST: print "(@B@I%s@I@B)\tselectedRevision='@B%s@B';  selectedLabel='@B%s@B'" % (__name__, selectedRevision, selectedLabel)
		return (selectedRevision, selectedLabel)
	else:
		return (None, None)
